package com.openroom.ui.component;

import java.net.URL;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.collections.Map;
import org.apache.pivot.util.Resources;
import org.apache.pivot.wtk.Border;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ComponentTooltipListener;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.LinkButton;
import org.apache.pivot.wtk.Tooltip;
import org.apache.pivot.wtk.content.ButtonData;

import com.openroom.event.ApplicationEvent;
import com.openroom.event.EventHandler;
import com.openroom.event.EventManager;
import com.openroom.event.EventType;
import com.openroom.model.ModelLocator;
import com.openroom.model.ModelLocator.ModeFlag.NavigationMode;
import com.openroom.ui.FilterButtonListener;
import com.openroom.ui.MainWindow;
import com.openroom.ui.NamespaceHelper;
import com.openroom.ui.UIComponent;

public class FilterPanel extends Border implements UIComponent {
    @BXML
    private LinkButton oneStarTnailFilterBtn;
    @BXML
    private LinkButton twoStarTnailFilterBtn;
    @BXML
    private LinkButton threeStarTnailFilterBtn;
    @BXML
    private LinkButton fourStarTnailFilterBtn;
    @BXML
    private LinkButton fiveStarTnailFilterBtn;
    @BXML
    private Label counterLabel;

    @BXML
    private LinkButton prevPage;

    @BXML
    private LinkButton nextPage;

    URL resource = MainWindow.class.getClassLoader().getResource(
            "com/openroom/ui/green_star.png");
    URL unselected = MainWindow.class.getClassLoader().getResource(
            "com/openroom/ui/white_star.png");

    MainWindow mainWindow;

    public FilterPanel() {
    }

    @Override
    public void initialize(Map<String, Object> namespace, URL arg1,
            Resources arg2, MainWindow window) {
        NamespaceHelper.initialize(namespace, this);
        this.mainWindow = window;
        oneStarTnailFilterBtn.getButtonPressListeners().add(
                new FilterButtonListener(this));
        twoStarTnailFilterBtn.getButtonPressListeners().add(
                new FilterButtonListener(this));
        threeStarTnailFilterBtn.getButtonPressListeners().add(
                new FilterButtonListener(this));
        fourStarTnailFilterBtn.getButtonPressListeners().add(
                new FilterButtonListener(this));
        fiveStarTnailFilterBtn.getButtonPressListeners().add(
                new FilterButtonListener(this));

        EventManager.getInstance().listenToEventType(new EventHandler() {
            @Override
            public void handle(ApplicationEvent event) {
                ModelLocator instance = ModelLocator.getInstance();
                int lastRating = 0;
                if (instance.getCurrentMode().navigationMode == NavigationMode.FOLDER) {
                    lastRating = instance.getCurrentlySelectedFolder()
                            .getLastRating();
                } else {
                    lastRating = instance.getCurrentlySelectedCatalog()
                            .getLastRating();
                }
                resetColor(unselected, oneStarTnailFilterBtn,
                        twoStarTnailFilterBtn, threeStarTnailFilterBtn,
                        fourStarTnailFilterBtn, fiveStarTnailFilterBtn);
                switch (lastRating) {
                case 5:
                    resetColor(resource, fiveStarTnailFilterBtn);
                case 4:
                    resetColor(resource, fourStarTnailFilterBtn);
                case 3:
                    resetColor(resource, threeStarTnailFilterBtn);
                case 2:
                    resetColor(resource, twoStarTnailFilterBtn);
                case 1:
                    resetColor(resource, oneStarTnailFilterBtn);
                    break;
                default:
                    break;
                }
                updateNumberOfImageText();
            }

        }, EventType.CURRENT_FOLDER_CHANGED, EventType.CURRENT_CATALOG_CHANGED);
        EventManager.getInstance().listenToEventType(new EventHandler() {
            @Override
            public void handle(ApplicationEvent event) {
                updateNumberOfImageText();
            }
        }, EventType.ALL_SHOWING_IMAGE_CHANGED, EventType.ALL_IMAGE_CHANGED);
        oneStarTnailFilterBtn.getUserData().put("RATING_VALUE", 1);
        twoStarTnailFilterBtn.getUserData().put("RATING_VALUE", 2);
        threeStarTnailFilterBtn.getUserData().put("RATING_VALUE", 3);
        fourStarTnailFilterBtn.getUserData().put("RATING_VALUE", 4);
        fiveStarTnailFilterBtn.getUserData().put("RATING_VALUE", 5);
        prevPage.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button arg0) {
                ModelLocator.getInstance().previousPage();
            }
        });

        nextPage.getButtonPressListeners().add(new ButtonPressListener() {

            @Override
            public void buttonPressed(Button arg0) {
                ModelLocator.getInstance().nextPage();
            }
        });

        EventManager.getInstance().listenToEventType(
                EventType.ALL_SHOWING_IMAGE_CHANGED, new EventHandler() {

                    @Override
                    public void handle(ApplicationEvent event) {
                        ModelLocator instance = ModelLocator.getInstance();
                        nextPage.setEnabled(instance.isNextPageAvailable());
                        prevPage.setEnabled(instance.isPreviousPageAvailable());
                    }
                });

    }

    private void updateNumberOfImageText() {
        int size = ModelLocator.getInstance().getAllPhotographsOfFolder()
                .size();
        int max = ModelLocator.MAX_IMAGE_PER_PAGE;
        if (size > max) {
            int currentPage = ModelLocator.getInstance().getCurrentPage();
            int minCount = Math.max(0, (currentPage - 1) * max);
            int maxCount = Math.min(currentPage * max, size);
            counterLabel.setText(String.format("Showing %d-%d of %d", minCount,
                    maxCount, size));
        } else {
            counterLabel.setText(String.format("Showing %d-%d of %d", size,
                    size, size));
        }
    }

    public void changeRating(int value) {
        resetColor(unselected, oneStarTnailFilterBtn, twoStarTnailFilterBtn,
                threeStarTnailFilterBtn, fourStarTnailFilterBtn,
                fiveStarTnailFilterBtn);
        switch (value) {
        case 5:
            resetColor(resource, fiveStarTnailFilterBtn);
        case 4:
            resetColor(resource, fourStarTnailFilterBtn);
        case 3:
            resetColor(resource, threeStarTnailFilterBtn);
        case 2:
            resetColor(resource, twoStarTnailFilterBtn);
        case 1:
            resetColor(resource, oneStarTnailFilterBtn);
        default:
            break;
        }
        switch (value) {
        case 0:
            oneStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            twoStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            threeStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE",
                    0);
            fourStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            fiveStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            break;
        case 1:
            oneStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 1);
            twoStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            threeStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE",
                    0);
            fourStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            fiveStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            break;
        case 2:
            oneStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            twoStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 2);
            threeStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE",
                    0);
            fourStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            fiveStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            break;
        case 3:
            oneStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            twoStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            threeStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE",
                    3);
            fourStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            fiveStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            break;
        case 4:
            oneStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            twoStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            threeStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE",
                    0);
            fourStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 4);
            fiveStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            break;
        case 5:
            oneStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            twoStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE", 0);
            threeStarTnailFilterBtn.getUserData().put("PREVIOUS_RATING_VALUE",
                    0);
            fourStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 0);
            fiveStarTnailFilterBtn.getUserData()
                    .put("PREVIOUS_RATING_VALUE", 5);
            break;

        default:
            break;
        }
    }

    private void resetColor(URL resource, LinkButton... buttons) {
        for (LinkButton linkButton : buttons) {
            ButtonData buttonData = (ButtonData) linkButton.getButtonData();
            buttonData.setIcon(resource);
            linkButton.repaint();
        }
    }
}
